#include "network.h"
#include "gui.h"
#include <sstream>



Client::Client( QObject* parent ): QTcpSocket( parent )
{
	block_size = 0;
	connect( this, SIGNAL( readyRead() ), this, SLOT( read_block() ) );
};



Client::~Client()
{
	this->disconnectFromHost();
};



QString* Client::read_string()
{
	QString* noth = new QString( "" );
	QDataStream in( this );
	in.setVersion( QDataStream::Qt_4_5 );
	if ( block_size == 0 )
	{
		if ( bytesAvailable() < ( int )sizeof( quint16 ) )
			return noth;
		in >> block_size;
	}
	if ( bytesAvailable() < block_size )
		return noth;
	QString* s = new QString();
	in >> *s;
	block_size = 0;
	return s;
};



void Client::read_block()
{
	QString* message = read_string();
	if( *message == "" )
		return;
	game_gui::get_instance()->g->buy_w->network_w->status_l->setText( *message );
};



void parse( QString* s )
{
	std::istringstream in( s->toStdString() );
	string type;
	in >> type;
	if( type == "SET_PLAYER" )
	{
		//PLAYER_NUMBER PLAYER_NAME NUMER_UNITS
		unsigned int pl_number;
		in >> pl_number;
		string pl_name;
		in >> pl_name;
		unsigned int pl_nm_units;
		in >> pl_nm_units;


	}
	else if ( type == "SET_UNIT" )
	{

	}
	else if( type == "ACTION" )
	{

	}
};



network_client* network_client::get_instance()
{
	static network_client inst( 0 );
	return &inst;
};